<template>
	<view class="pages-technician">
		<fixed @height="initFixHeight" :initHeight="configInfo.coach_format">
			<uni-nav-bar :fixed="false" :shadow="false" :statusBar="true" :onlyLeft="true" :color="`#fff`"
				:backgroundColor="primaryColor">
				<view @tap.stop="toChooseLocation" class="map-info flex-y-center" slot="left">
					<view class="flex-y-center c-base">
						<i class="iconfont iconjuli mr-sm"></i>
						<view class="map-text max-400 ellipsis">
							{{location&&location.address ?location.address : isLoad ? '定位失败' : '定位中...'}}
						</view>
						<i class="iconfont icon-down"></i>
					</view>
				</view>
			</uni-nav-bar>
			<view class="fix-info rel" :class="[{'fill-base':cityIndex==-1},{'fill-body':cityIndex!==-1}]"
				:style="{height:configInfo.coach_format == 2?'230rpx':'330rpx'}">
				<view class="space-top abs" :style="{background:primaryColor}"></view>
				<view class="abs" style="width:100%;top:0">
					<view class="search-info fill-base pt-lg pl-lg pr-lg"
						:class="[{'pb-md':configInfo.coach_format!=2}]">
						<view class="flex-center"
							:class="[{'pb-md':configInfo.coach_format!=2},{'pb-lg':configInfo.coach_format==2}]">
							<view class="city-info">
								<picker @change="pickerChange($event)" :value="cityIndex" :range="cityList"
									range-key="title">
									<view class="city-info flex-y-center">
										<view>
											<block v-if="cityList&&cityList.length > 0">
												{{cityIndex === -1 ? '未知' : cityList[cityIndex].title.length > 3 ? cityList[cityIndex].title.substring(0,3) + '...':cityList[cityIndex].title}}
											</block>
											<block v-else>请选择</block>
										</view>
										<i class="iconfont iconshaixuanxia-1 ml-sm"></i>
									</view>
								</picker>
							</view>
							<view class="flex-1">
								<search @input="toSearch" type="input" :keyword="param.coach_name" :padding="0"
									:radius="30" backgroundColor="#F0F0F0"
									:placeholder="'请输入'+$t('action.attendantName')+'姓名'">
								</search>
							</view>
						</view>
						<block v-if="configInfo.coach_format != 2">
							<tab @change="handerTabChange" :list="tabList" :activeIndex="activeIndex*1"
								:activeColor="primaryColor" width="25%" height="90rpx" fontSize="28rpx"></tab>
						</block>
					</view>
					<view class="search-cate-list flex-warp mt-md ml-md mr-md pb-md">
						<view @tap.stop="toChangeItem('recommend', check.recommend == 0 ? 1 : 0,1)"
							class="item-child flex-center f-desc radius-16 mr-lg"
							:style="{background:check.recommend?primaryColor:'',color:check.recommend?'#fff':''}"
							v-if="configInfo.plugAuth.recommend">
							<view class="ellipsis">推荐{{$t('action.attendantName')}}</view>
						</view>
						<view @tap.stop="toChangeItem('cate_id',item.id,1)"
							class="item-child flex-center f-desc radius-16 mr-lg"
							:style="{background:check.cate_id == item.id ?primaryColor:'',color:check.cate_id == item.id?'#fff':''}"
							v-for="(item,index) in serviceCateList" :key="index"
							v-show="(configInfo.plugAuth.recommend && index < 2) || (!configInfo.plugAuth.recommend && index < 3)">
							<view class="ellipsis">{{item.title}}</view>
						</view>
						<view @tap.stop="toPopup('search_item','open')" class="item-child flex-center f-desc radius-16">
							更多筛选</view>
					</view>
				</view>
			</view>
		</fixed>

		<view class="ml-md mr-md" :class="[{'flex-warp':configInfo.coach_list_format===2}]"
			v-if="pageActive && list.data && list.data.length>0">

			<view
				:class="[{'mt-md':(configInfo.coach_list_format===1&&index!=0) || (configInfo.coach_list_format===2 && index>1)},{'mr-md':configInfo.coach_list_format===2 && index%2==0}]"
				v-for="(item,index) in list.data" :key="index">
				<technician-list-item @comment="toShowPopup(index,'message')" @collect="toCollect(index)"
					@order="toShowPopup(index,'technician')" :info="item">
				</technician-list-item>
			</view>

		</view>


		<!-- #ifdef H5 -->
		<load-more :noMore="list.current_page>=list.last_page&&list.data.length>0" :loading="loading" v-if="loading">
		</load-more>
		<abnor v-if="!loading&&list.data.length<=0&&list.current_page==1&&cityIndex!==-1"></abnor>
		<!-- #endif -->
		<!-- #ifndef H5 -->
		<load-more :noMore="list.current_page>=list.last_page&&list.data.length>0&&location.lng" :loading="loading"
			v-if="loading">
		</load-more>


		<abnor v-if="!loading&&list.data.length<=0&&list.current_page==1&&location.lng&&cityIndex!==-1"></abnor>
		<abnor @confirm="toOpenLocation" :tip="[{ text: '定位失败，请开启地理位置授权后刷新页面重试~', color: 0 }]"
			:button="[{ text: '开启定位' , type: 'confirm' }]" btnSize="" v-if="!loading && !location.lng"> </abnor>
		<!-- #endif -->

		<view class="fill-base fix" :style="{top:`${popupHeight}px`,bottom:`${configInfo.tabbarHeight}px`}"
			v-if="!loading&&list.data.length<=0&&list.current_page==1&&cityIndex===-1">
			<open-city-item ref="open_city_item" :height="popupHeight"></open-city-item>
		</view>

		<view class="space-footer"></view>

		<technician-list-popup ref="technician_list_popup"></technician-list-popup>

		<uni-popup ref="choose_city_item" type="top" :custom="true" :zIndex="999">
			<view :style="{height:configInfo.navBarHeight + 8 + 'px'}"></view>
			<view class="choose-city-popup rel">
				<image class="city-img" src="https://lbqny.migugu.com/admin/anmo/technician/no_city.png"></image>
				<view class="text f-paragraph c-base abs">
					很抱歉,该城市未开通, 点击左上角切换其他城市下单服务
				</view>
				<view class="flex-between">
					<view></view>
					<view @tap.stop="$refs.choose_city_item.close()"
						class="item-btn flex-center f-paragraph c-title text-bold radius">知道了</view>
				</view>
			</view>
		</uni-popup>

		<uni-popup @change="toChangePopupShow" ref="search_item" type="top" left="164rpx" :custom="true" :zIndex="999">
			<view :style="{height:configInfo.navBarHeight + 'px'}"></view>
			<view class="search-popup pd-lg fill-base">
				<view class="f-desc c-title text-bold">{{$t('action.attendantName')}}性别
				</view>
				<view class="flex-warp">
					<view @tap.stop="toChangeItem('sex',item.id)"
						class="list-item flex-center mt-md mr-md f-paragraph c-paragraph radius"
						:style="{background:check.sex==item.id?primaryColor:'',color:check.sex==item.id?'#fff':''}"
						v-for="(item,index) in sexList" :key="index">{{item.title}}
					</view>
				</view>
				<view class="f-desc c-title text-bold pt-lg mt-md">服务分类
				</view>
				<view class="flex-warp" style="max-height: 37vh;overflow: auto;">
					<view @tap.stop="toChangeItem('cate_id',item.id)"
						class="list-item flex-center mt-md mr-md f-paragraph c-paragraph radius"
						:style="{background:check.cate_id==item.id?primaryColor:'',color:check.cate_id==item.id?'#fff':''}"
						v-for="(item,index) in serviceCateList" :key="index">{{item.title}}
					</view>
				</view>
				<view class="f-desc c-title text-bold pt-lg mt-md">服务类型
				</view>
				<view class="flex-warp">
					<view @tap.stop="toChangeItem('service_type',item.id)"
						class="list-item flex-center mt-md mr-md f-paragraph c-paragraph radius"
						:style="{background:check.service_type==item.id?primaryColor:'',color:check.service_type==item.id?'#fff':''}"
						v-for="(item,index) in serviceTypeList" :key="index">{{item.title}}
					</view>
				</view>
				<block v-if="configInfo.plugAuth.store">
					<view class="f-desc c-title text-bold pt-lg mt-md">个性筛选
					</view>
					<input type="text" v-model="check.store_name" placeholder="请输入店铺名称"
						class="list-item text-center mt-md f-paragraph c-paragraph radius" style="width:100%" />
				</block>
				<view style="height:60rpx"></view>
				<view class="flex-center">
					<view @tap.stop="toReset" class="search-btn flex-center f-mini-title c-title radius">重置</view>
					<view @tap.stop="toConfirm" class="search-btn flex-center ml-lg f-mini-title c-base radius"
						:style="{background:primaryColor,borderColor:primaryColor}">确定</view>
				</view>
			</view>
		</uni-popup>


		<view :style="{height: `${configInfo.tabbarHeight}px`}"></view>
		<tabbar :cur="2"></tabbar>

		<!-- #ifdef APP-PLUS -->
		<login-info></login-info>
		<!-- #endif -->
		<change-user-type></change-user-type>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	import siteInfo from '@/siteinfo.js';
	import tabbar from "@/components/tabbar.vue"
	import technicianListItem from "@/components/technician-list-item.vue"
	import technicianListPopup from "@/components/technician-list-popup.vue"
	import openCityItem from "@/components/open-city-item.vue"
	export default {
		components: {
			tabbar,
			technicianListItem,
			technicianListPopup,
			openCityItem
		},
		data() {
			return {
				isLoad: false,
				options: {},
				popupHeight: '',
				loading: true,
				lockTap: false,
			}
		},
		computed: mapState({
			pageActive: state => state.technician.pageActive,
			haveOperItem: state => state.technician.haveOperItem,
			cityId: state => state.technician.cityId,
			cityIndex: state => state.technician.cityIndex,
			cityList: state => state.technician.cityList,
			activeIndex: state => state.technician.activeIndex,
			tabList: state => state.technician.tabList,
			sexList: state => state.technician.sexList,
			serviceCateList: state => state.technician.serviceCateList,
			serviceTypeList: state => state.technician.serviceTypeList,
			check: state => state.technician.check,
			param: state => state.technician.param,
			list: state => state.technician.list,
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			configInfo: state => state.config.configInfo,
			loginType: state => state.user.loginType,
			userInfo: state => state.user.userInfo,
			location: state => state.user.location,
			changeAddr: state => state.user.changeAddr,
			isGzhLogin: state => state.user.isGzhLogin,
			haveShieldOper: state => state.user.haveShieldOper,
		}),
		async onLoad(options) {
			console.log("====onLoad technician")
			this.options = options
			let {
				pid = 0,
					city_id = 0,
					coach_id = 0,
					coach_name = ''
			} = options

			let param = this.$util.deepCopy(this.param)
			param.coach_id = coach_id
			param.coach_name = coach_name
			this.updateTechnicianItem({
				key: 'param',
				val: param
			})

			if (coach_id) {
				this.updateTechnicianItem({
					key: 'cityId',
					val: city_id
				})
			}
			// #ifdef H5
			uni.setNavigationBarTitle({
				title: this.$t('action.attendantName')
			})
			// #endif 

			let {
				data = []
			} = this.list
			let city_refresh = data.length == 0 || data[0].city_id != this.cityId

			let {
				realtime_location = 0
			} = this.configInfo
			let {
				pageActive,
				changeAddr
			} = this
			if (pageActive && (!realtime_location && !changeAddr) && (!coach_id || !pid) && !city_refresh) {
				this.isLoad = true
				this.loading = false
				this.$util.hideAll()
				return
			}
			await this.initIndex()
		},
		async onShow() {
			console.log("====onShow technician")
			// #ifdef H5
			if (this.$jweixin.isWechat()) {
				await this.$jweixin.initJssdk();
				this.toAppShare()
			}
			// #endif 
			if (this.haveShieldOper == 2 || this.haveOperItem) {
				this.initIndex()
				this.updateUserItem({
					key: 'haveShieldOper',
					val: 0
				})
				this.updateTechnicianItem({
					key: 'haveOperItem',
					val: false
				})
			}
		},
		onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif 
			this.updateUserItem({
				key: 'changeAddr',
				val: false
			})
			this.initRefresh();
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if (this.list.current_page >= this.list.last_page || this.loading) return;
			this.loading = true;
			this.getList(this.param.page + 1);
		},
		onShareAppMessage(e) {
			let {
				id: pid = 0
			} = this.userInfo
			let path = `/pages/technician?pid=${pid}`
			this.$util.log(path)
			return {
				title: '',
				imageUrl: '',
				path,
			}
		},
		methods: {
			...mapActions(['getConfigInfo', 'getUserInfo', 'getServiceCate', 'getCityList', 'getServiceCoachList']),
			...mapMutations(['updateUserItem', 'updateServiceItem', 'updateTechnicianItem', 'updateMapItem']),
			async initIndex(refresh = false) {
				let {
					pid = 0
				} = this.options
				let {
					isGzhLogin
				} = this
				let {
					id: uid = 0
				} = this.userInfo
				if (pid && !uid) {
					// #ifdef H5
					if (isGzhLogin) {
						setTimeout(() => {
							this.getUserInfo()
						}, 1000)
					} else {
						this.getUserInfo()
					}
					// #endif
					// #ifndef H5
					await this.getUserInfo()
					// #endif 
				}

				if (!this.configInfo.id || refresh) {
					await this.getConfigInfo()
				}
				await this.getServiceCate()
				await this.getList(1, true)
				this.updateTechnicianItem({
					key: 'pageActive',
					val: true
				})
			},
			initRefresh() {
				this.param.page = 1
				this.initIndex(true)
			},
			toAppShare() {
				let {
					id: pid = 0
				} = this.userInfo
				let title = this.$t('action.attendantName')
				let {
					siteroot
				} = siteInfo
				let url = siteroot.split('/index.php')[0]
				let href = `${url}/h5/#/pages/technician?pid=${pid}`
				let imageUrl = ''
				this.$jweixin.wxReady(() => {
					this.$jweixin.showOptionMenu()
					this.$jweixin.shareAppMessage(title, '', href, imageUrl)
					this.$jweixin.shareTimelineMessage(title, href, imageUrl)
				})
			},
			initFixHeight(val) {
				this.popupHeight = val
			},
			pickerChange(e, val) {
				let ind = e.target.value
				this.updateTechnicianItem({
					key: 'cityIndex',
					val: ind
				})
				this.updateTechnicianItem({
					key: 'cityId',
					val: this.cityList[ind].id
				})
				this.getList(1)
			},
			toOpenLocation() {
				this.$util.checkAuth({
					type: 'userLocation'
				})
				// #ifdef APP-PLUS
				uni.getSystemInfo({
					success: (sys) => {
						if (sys.platform == 'ios') {
							plus.runtime.openURL("app-settings://");
						} else {
							var main = plus.android.runtimeMainActivity();
							var Intent = plus.android.importClass(
								"android.content.Intent");
							//可能应该直接进入应用列表的权限设置？=> android.settings.APPLICATION_SETTINGS  
							var mIntent = new Intent(
								'android.settings.LOCATION_SOURCE_SETTINGS'
							);
							main.startActivity(mIntent);
						}
					}
				})
				// #endif
			},
			toSearch(val) {
				let param = this.$util.deepCopy(this.param)
				param.coach_id = ''
				param.coach_name = val
				this.updateTechnicianItem({
					key: 'param',
					val: param
				})
				this.getList(1)
			},
			// 选择地区
			async toChooseLocation(e) {
				await this.$util.checkAuth({
					type: 'userLocation'
				})

				// #ifdef H5
				if (this.$jweixin.isWechat()) {
					await this.$jweixin.wxReady2();
					let {
						latitude,
						longitude
					} = await this.$jweixin.getWxLocation()
					this.toOpenChooseLocation({
						latitude,
						longitude
					})
				}
				// #endif
				// #ifdef APP-PLUS
				let {
					lat,
					lng
				} = await this.$util.getBmapLocation()
				this.toOpenChooseLocation({
					latitude: lat,
					longitude: lng
				})
				// #endif 
				// #ifdef MP-WEIXIN
				this.toOpenChooseLocation()
				// #endif
			},
			async toOpenChooseLocation(param = {}) {
				let [, {
					name = '',
					address = '',
					longitude: lng,
					latitude: lat,
					province = '',
					city = '',
					district = '',
				}] = await uni.chooseLocation(param);
				if (!lng) return
				let location = {
					lng,
					lat,
					address: address || name,
					province,
					city,
					district
				}
				this.updateUserItem({
					key: 'location',
					val: location
				})
				this.updateUserItem({
					key: 'changeAddr',
					val: true
				})
				if (this.$refs.open_city_item) {
					this.$refs.open_city_item.city = ''
				}
				let {
					coach_id: cid = 0
				} = this.param
				await this.getCityList({
					lng,
					lat,
					change: cid ? 0 : 1
				})
				await this.getList(1)
			},
			async getList(page = 0, refresh = false) {
				if (page) {
					let param = this.$util.deepCopy(this.param)
					param.page = page
					this.updateTechnicianItem({
						key: 'param',
						val: param
					})
				}
				let {
					location,
					changeAddr
				} = this

				let {
					realtime_location = 0
				} = this.configInfo
				if (!location.lat || (location.lat && location.address == '定位失败') || (refresh && realtime_location && !
						changeAddr)) {
					// #ifdef H5
					if (this.$jweixin.isWechat()) {
						if (!location.lat) {
							this.$util.showLoading()
						}
						// await this.$jweixin.initJssdk();
						await this.$jweixin.wxReady2();
						let {
							latitude: lat = 0,
							longitude: lng = 0
						} = await this.$jweixin.getWxLocation()
						let locaParams = Object.assign({}, location, {
							lat,
							lng,
							address: '定位失败'
						})
						location = locaParams
						if (lat && lng) {
							let key = `${lat},${lng}`
							let data = await this.$api.base.getMapInfo({
								location: key
							})
							let {
								status,
								result
							} = JSON.parse(data)
							if (status == 0) {
								let {
									address_component,
									formatted_addresses
								} = result
								let {
									standard_address: address
								} = formatted_addresses
								let {
									province,
									city,
									district
								} = address_component
								location = {
									lat,
									lng,
									address,
									province,
									city,
									district
								}
							}
						}
					}
					// #endif
					// #ifndef H5
					location = await this.$util.getBmapLocation()
					// #endif
					this.updateUserItem({
						key: 'location',
						val: location
					})
				}
				let {
					lng = 0,
						lat = 0
				} = location

				if (refresh) {
					let {
						coach_id: cid = 0
					} = this.param
					await this.getCityList({
						lng,
						lat,
						change: cid ? 0 : 1
					})
				}

				let {
					list: oldList,
					activeIndex,
					tabList,
					cityList,
					cityIndex,
					cityId: city_id
				} = this

				let param = this.$util.deepCopy(this.param)

				let ind = cityList.findIndex(item => {
					return item.id == city_id
				})

				if (ind == -1) {
					// this.$refs.choose_city_item.open()
					if (this.$refs.open_city_item) {
						this.$refs.open_city_item.initIndex()
					}
					this.loading = false
					this.$util.hideAll()
					this.updateTechnicianItem({
						key: 'list',
						val: {
							data: [],
							last_page: 1,
							current_page: 1
						}
					})
					return
				}

				city_id = ind == -1 ? 0 : city_id
				cityIndex = ind == -1 ? 0 : ind


				this.updateTechnicianItem({
					key: 'cityIndex',
					val: cityIndex
				})

				this.updateTechnicianItem({
					key: 'cityId',
					val: city_id
				})

				param = Object.assign({}, param, this.check, {
					lat,
					lng,
					city_id
				})

				let {
					coach_format = 1
				} = this.configInfo

				if (coach_format == 1) {
					param.type = tabList[activeIndex].id
				}

				if (param.sex === -1) {
					delete param.sex
				}
				if (param.recommend === 0) {
					delete param.recommend
				}

				if (param.cate_id === 0) {
					delete param.cate_id
				}

				if (param.service_type === 0) {
					delete param.service_type
				}

				await this.getServiceCoachList(param)
				this.loading = false
				this.$util.hideAll()
			},
			handerTabChange(index) {
				this.updateTechnicianItem({
					key: 'activeIndex',
					val: index
				})
				this.$util.showLoading()
				uni.pageScrollTo({
					scrollTop: 0
				})
				this.getList(1)
			},
			toChangeItem(key, id, flag = 0) {
				if (key === 'cate_id' && this.check[key] == id) {
					id = 0
				}
				let checkVal = Object.assign({}, this.check, {
					[key]: id
				})
				this.updateTechnicianItem({
					key: 'check',
					val: checkVal
				})
				if (!flag) return
				this.toConfirm(0)
			},
			toPopup(refs_key, method) {
				this.$refs[refs_key][method]()
				this.popupTap = method === 'open' ? true : false
			},
			toChangePopupShow(e) {
				let {
					show
				} = e
				if (show) return
				this.updateTechnicianItem({
					key: 'check',
					val: this.$util.pick(this.param, ['sex', 'cate_id', 'recommend', 'service_type', 'store_name'])
				})
			},
			toReset() {
				this.updateTechnicianItem({
					key: 'check',
					val: {
						sex: -1,
						cate_id: 0,
						recommend: 0,
						service_type: 0,
						store_name: ''
					}
				})
				this.toConfirm()
			},
			toConfirm(flag = 1) {
				let param = Object.assign({}, this.param, this.check)
				this.updateTechnicianItem({
					key: 'param',
					val: param
				})
				this.getList(1)
				if (!flag) return
				this.$refs.search_item.close()
				this.popupTap = false
			},
			async toShowPopup(index, key) {
				this.$refs.technician_list_popup.toShowPopup(this.list.data[index], key)
			},
			async toCollect(index) {
				let {
					id,
					is_collect,
					collect_num
				} = this.list.data[index]
				let methodModel = is_collect ? 'delCollect' : 'addCollect'
				await this.$api.mine[methodModel]({
					coach_id: id
				})
				this.$util.showToast({
					title: is_collect ? '取消成功' : '收藏成功'
				})
				this.list.data[index].is_collect = is_collect == 1 ? 0 : 1
				this.list.data[index].collect_num = is_collect == 1 ? collect_num - 1 :
					collect_num + 1
			}
		}
	}
</script>


<style lang="scss">
	.search-popup {
		width: calc(100% - 164rpx);
		height: 100vh;

		.list-item {
			width: 160rpx;
			height: 70rpx;
			background: #F5F5F5;

			.icon-guanbi-fill.abs {
				top: -15rpx;
				right: 0;
				font-size: 50rpx;
			}
		}

		.list-item:nth-child(3n) {
			margin-right: 0;
		}

		.search-btn {
			width: 230rpx;
			height: 80rpx;
			border: 1rpx solid #C7C7C7;
			transform: rotateZ(360deg);
		}
	}
</style>